package com.googlecode.KeepInTouch;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class KeepInTouchApp extends Activity {
	private static final String TAG = "KeepInTouchApp";
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MMM/dd hh:mm a");
	
	@Override
	public void onCreate(Bundle savedState) {
		Log.d(TAG, "onCreate");
		super.onCreate(savedState);
		setContentView(R.layout.main);
	}
	
	@Override
	public void onStart() {
		Log.d(TAG, "onStart");
		super.onStart();
		showCallLogSummary();
	}
	
	private void showCallLogSummary() {
		Log.d(TAG, "showCallLogSummary");
		TextView tv = (TextView)findViewById(R.id.tvMain);
		Collection<ContactSummary> summary = CallLogAnalyser.getSummary(this);
		Iterator iter = summary.iterator();
		StringBuilder sb = new StringBuilder();
		for( ; iter.hasNext(); ) {
			ContactSummary entry = (ContactSummary)iter.next();
			sb.append(entry.id + ",");
			sb.append(entry.lookupKey + ",");
			sb.append(entry.calls + ",");
			sb.append(entry.durationSum + ",");
			sb.append(dateFormat.format(new Date(entry.lastCallDate)) + ",");
			sb.append(entry.incomingCalls + ",");
			sb.append(entry.incomingSum + ",");
			sb.append(dateFormat.format(new Date(entry.lastIncomingCall)) + ",");
			sb.append(entry.outgoingCalls + ",");
			sb.append(entry.outgoingSum + ",");
			sb.append(dateFormat.format(new Date(entry.lastOutgoingCall)) + ",");
			sb.append(entry.missedCalls + ",");
			sb.append(dateFormat.format(new Date(entry.lastMissedCall)) + "\n");
    	}
    	tv.append(sb.toString());
	}

}
